package testes;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import visualcbir.util.VSUtils;

public class CircularShapesTeste2 extends JPanel implements MouseListener, MouseMotionListener{
    Graphics oGraphicsOff;
    Image oImageScreenOff;
    Dimension oDimensionScreensizeOff;

    public CircularShapesTeste2(){
        this.addMouseListener(this);
        //this.addMouseMotionListener(this);
    }

    public void paintComponent(Graphics g)
    {
       System.out.println("PAINTTTT");
       //********************** Creates an empty initial image ***************//
        Dimension oDimension = this.getSize();
        if((oImageScreenOff == null) || (oDimension.width != oDimensionScreensizeOff.width) || (oDimension.height != oDimensionScreensizeOff.height))
        {  oImageScreenOff = createImage(oDimension.width, oDimension.height);
           oDimensionScreensizeOff = oDimension;
           if (oGraphicsOff != null)    oGraphicsOff.dispose();
           oGraphicsOff = oImageScreenOff.getGraphics();
           oGraphicsOff.setFont(getFont());
        }

        oGraphicsOff.setColor(getBackground());
        oGraphicsOff.fillRect(0, 0, oDimension.width, oDimension.height);

       //*********************************************************************//
       //************** ALL is drawed over the empty image********************//
        oGraphicsOff.setColor(Color.black);
        //this.setBackground(Color.white);
        try {
            drawSpiralArchimedes2(oGraphicsOff);
            //oGraphicsOff.draw
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CircularShapesTeste2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CircularShapesTeste2.class.getName()).log(Level.SEVERE, null, ex);
        }

        //oGraphicsOff.draw

        //******Finally the used image is drawed into the actual screen*******//
        boolean bTeste = g.drawImage(oImageScreenOff, 0, 0, null);//boolean bTeste= //new Color(255,255,255),
        //System.out.println("cambio->" + bTeste);
    }
    public class Node{
        double x;
        double y;

        public Node(double x, double y) {
            this.x = x;
            this.y = y;
        }
        
    }
    public class Edge{
        Node oNodefrom;
        Node oNodeto;

        public Edge(Node oNodefrom, Node oNodeto) {
            this.oNodefrom = oNodefrom;
            this.oNodeto = oNodeto;
        }

    }
       ArrayList<Node> aNodes=new ArrayList();
       ArrayList<Edge> aEdges=new ArrayList();


        

     Node oNodeNew,oNodeTemp;
     public void drawSpiralArchimedes2(Graphics g) throws FileNotFoundException, IOException{
       //oGraphicsOff.drawImage(VSUtils.readImage("/home/sheila/NetBeansProjects/data/ATT_faces/s1/1.pgm"), 100, 100, null); // cuidado con los observers
       //g.drawImage(VSUtils.readImage("/home/sheila/NetBeansProjects/data/ATT_faces/s1/1.jpg"), 200, 200, null);

         //*********++++aqui temporal*************************************+++++++
        //********************** r = k * Theta^ a ****************************//
        Graphics2D g2d=(Graphics2D)g;
        double xcenter=this.getWidth()/2, ycenter=this.getHeight()/2;
        double x,y,radio, k=2.5, a=1.5,numRotations=5;
        double ThetaRadianes=0.0;
        GeneralPath aGeneralPath=new GeneralPath();
        aGeneralPath.moveTo(xcenter, ycenter);

        for(double Theta=0;Theta<360*numRotations;Theta+=2){
            ThetaRadianes=Theta*(Math.PI/180);
            radio = k * Math.pow(ThetaRadianes,a);
            x=radio*Math.cos(ThetaRadianes)+xcenter;
            y=radio*Math.sin(ThetaRadianes)+ycenter;
            
            //g2d.fillOval((int)Math.round(x),(int)Math.round(y),2,2);
            aGeneralPath.lineTo(x, y);
            if((Theta%30) == 0){
                if(oNodeNew==null) oNodeTemp=new Node(xcenter,ycenter);
                oNodeTemp=oNodeNew;
                oNodeNew=new Node(x,y);
                aNodes.add(oNodeNew);
                aEdges.add(new Edge(oNodeTemp,oNodeNew));
                
                g2d.drawString("aqui", (int)x, (int)y);
                g2d.fillOval((int)x, (int)y,3,3);
                System.out.println("x->"+x);
                System.out.println("y->"+y);
            }
            //g2d.draw();
            //aGeneralPath.curveTo(x, y, oNodeTemp.x, oNodeTemp.y);
        }
       // g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, null, 0.0f));
         g2d.draw(aGeneralPath);
         drawEdge(g2d,30);
         drawEdge(g2d,31);
         drawEdge(g2d,32);
         drawEdge(g2d,33);
         drawEdge(g2d,34);
         drawEdge(g2d,45);
         drawEdge(g2d,46);
         drawEdge(g2d,55);

         //PathIterator oPathIterator=aGeneralPath.getPathIterator(null);         oPathIterator.next();         
    }

     public void drawEdge(Graphics g,int iEdge){
         double x,y,radio, k=2.5, a=1.5;
         double ThetaRadianes=0.0;
         double xcenter=this.getWidth()/2, ycenter=this.getHeight()/2;
         GeneralPath aGeneralPath=new GeneralPath();         
         
         Edge oEdgeTemp=aEdges.get(iEdge);
         aGeneralPath.moveTo((int)oEdgeTemp.oNodefrom.x, (int)oEdgeTemp.oNodefrom.y);
         g.drawOval((int)oEdgeTemp.oNodefrom.x, (int)oEdgeTemp.oNodefrom.y,10,10);

         double dComputatedRadio = Math.sqrt(Math.pow(oEdgeTemp.oNodefrom.x-xcenter, 2) + Math.pow(oEdgeTemp.oNodefrom.y-ycenter, 2));
         double dComputatedAngle = Math.pow(dComputatedRadio/k,1/a);
         System.out.println("dComputatedANgle->"+dComputatedAngle+ "  en cartesiano->"+dComputatedAngle*(180/Math.PI));
        //for(double Theta=0;Theta<360*numRotations;Theta+=2){
            //ThetaRadianes=Theta*(Math.PI/180);
         g.setColor(Color.red);

         ((Graphics2D)g).setStroke(new BasicStroke(5.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, null, 0.0f));
         
        for(double Theta=dComputatedAngle*(180/Math.PI);Theta<(((dComputatedAngle+(31*Math.PI/180))*(180/Math.PI)));Theta+=2){
            ThetaRadianes=Theta*(Math.PI/180);
            radio = k * Math.pow(ThetaRadianes,a);
            x=radio*Math.cos(ThetaRadianes)+xcenter;
            y=radio*Math.sin(ThetaRadianes)+ycenter;
            aGeneralPath.lineTo(x, y);
        }
         
         ((Graphics2D)g).draw(aGeneralPath);

         ((Graphics2D)g).setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, null, 0.0f));
            //g2d.fillOval((int)Math.round(x),(int)Math.round(y),2,2);
            
     }

    public double convertCartesianToPolar(double angleCartesian){
        return angleCartesian * (Math.PI/180);
    }

    //************************************************************************//
    //class MyMouseListener implements MouseListener, MouseMotionListener{
            Point oPointInitial;
            Point oPointFinal;

            //public MyMouseListener(Graphics pg){
                //   this.g=pg;
            //}


            public void mousePressed(MouseEvent e) {
                addMouseMotionListener(this);
                System.out.println("mousePressed");
                oPointInitial=new Point(e.getX(),e.getY());
                repaint();
                e.consume();
            }

            public void mouseReleased(MouseEvent e) {
                this.removeMouseMotionListener(this);
                System.out.println("mouseReleased");
                //oPointFinal=new Point(e.getX(),e.getY());
                //rotateSpiral(oPointInitial, oPointFinal);
                repaint();
                e.consume();
            }

            public void mouseEntered(MouseEvent e) {
                /*System.out.println("mouseEntered");
                ((Graphics2D)this.oGraphicsOff).rotate(5, 100, 100);
                this.oGraphicsOff.drawString("------------>SHEILA", 100, 100);
                ((Graphics2D)this.oGraphicsOff).rotate(-5, 100, 100);
                this.oGraphicsOff.drawString("REGRESE CON NEGATIVO<--------", 100, 100);
                this.oGraphicsOff.drawImage(oImageScreenOff, 0, 0, null);*/

            }
            public void mouseClicked(MouseEvent e) {    }
            public void mouseExited(MouseEvent e) {//contrario de mouseEntered
                System.out.println("mouseExited");
            }
            private Point oPointTemp;
            public void mouseDragged(MouseEvent e) {
                System.out.println("mouseDragged");
                Point oPointTempPre;
                oPointTempPre=(oPointTemp==null)?new Point(e.getX(),e.getY()):oPointTemp;

                oPointTemp=new Point(e.getX(),e.getY());
                rotateSpiral(oPointTempPre, oPointTemp);
                //this.rotateSpiral(oPointInitial, oPointTemp); //no seria oPointInitial
                repaint();
                e.consume();
            }
            public void rotateSpiral(Point oPointI, Point oPointF){
                try{
                double xcenter=CircularShapesTeste2.this.getWidth()/2;
                double ycenter=CircularShapesTeste2.this.getHeight()/2;
                Point oPointCenter=new Point((int)xcenter,(int)ycenter);

                double ang=this.getAngleRotation(oPointI, oPointF, oPointCenter);
                //double ang=5;
                ((Graphics2D)this.oGraphicsOff).rotate(ang, xcenter, ycenter);
                this.oGraphicsOff.drawString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", (int)xcenter, (int)ycenter);
                this.oGraphicsOff.drawImage(oImageScreenOff, 0, 0, null);
                this.oGraphicsOff.drawString("Centerrrrrrrrrrrrrrrrrrrrrrrrrrrr", (int)xcenter, (int)ycenter);

                }catch(Exception e){System.out.println("Error->"+e.getLocalizedMessage());}
            }
            public double getAngleRotation(Point oPointI, Point oPointF, Point oPointCenter){

                double base = Math.sqrt(Math.pow(oPointI.getX()-oPointF.getX(), 2)+Math.pow(oPointI.getY()-oPointF.getY(), 2));
                double hypotenuse = Math.sqrt(Math.pow(oPointI.getX()-oPointCenter.getX(), 2)+Math.pow(oPointI.getY()-oPointCenter.getY(), 2));

                double angle=Math.asin(base/(2*hypotenuse));//OJOOO REVISAR SI EL ARGUMENTO ES MAYOR QUE 1 O SI HAY DIVISION ENTRE 0 ETC.. TRYCATCH
                //if(angle<Math.PI)
                    angle=(+1)*angle;

                //angle=(angle<Math.PI)?-angle:angle;
                return angle;
            }

            public void mouseMoved(MouseEvent e) {

            }
//********************** UTILS ***********************************************//
    public void callTimer(){
         Timer timer2 = new Timer(5000,new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            System.out.println("Timer ejecutandose arriba");
        } });
        timer2.setRepeats(false);
        timer2.start();
    }

    public static void main(String[] args)
    {
        JFrame oJFrame= new JFrame();
        oJFrame.getContentPane().add(new CircularShapesTeste2());
        oJFrame.setSize(1100, 700);
        oJFrame.show();
    }

}